import { Popconfirm } from 'antd'
import { colors } from '@/desktop/styles'
import { useStreams } from '@/lib/utils'
import services from '@/services'
import type { ContactType } from '@/services/contacts'

/**
 * 联系人分类
 */
export function Category(props: {
  type: ContactType
  name: string
  isCurrent: boolean
  setType: (type: ContactType) => void
  unread: number
  resetUnread: (type: ContactType) => void
}) {
  const { type, name, isCurrent, setType, unread, resetUnread } = props
  const [observingReadonly] = useStreams(services.auth.observing.readonly$)
  const unreadContent = <span css={styles.unread}>{unread > 999 ? '999+' : unread}</span>
  return <div
    css={styles.category}
    className={isCurrent ? 'current ' : ''}
    onClick={() => setType(type)}
  >
    <div className="inner">
      {name}
      {unread ? (
        observingReadonly ? (
          unreadContent
        ) : (
          <Popconfirm title="确定将此类联系人标为已读吗？" onConfirm={() => resetUnread(type)}>
            {unreadContent}
          </Popconfirm>
        )
      ) : null}
    </div>
  </div>
}

const styles = {
  category: css`
    background-color: #f5f5f5;
    cursor: pointer;
    flex: 1;
    font-size: 16px;
    letter-spacing: 1px;
    line-height: 70px;
    position: relative;
    text-align: center;
    transition: all 0.2s ease-in;
    user-select: none;

    &:hover,
    &.current {
      background-color: #ffffff;
      color: ${colors.link};
    }

    // 保证 ”未读数量“ 超出 category 时，后一个 category 不会挡住前一个 category 的未读数
    // prettier-ignore
    &:nth-of-type(1) { z-index: 3; }
    // prettier-ignore
    &:nth-of-type(2) { z-index: 2; }
    // prettier-ignore
    &:nth-of-type(3) { z-index: 1; }

    .inner {
      display: inline-block;
      margin: 0 auto;
      position: relative;
    }
  `,

  unread: css`
    background-color: #e66551;
    border-radius: 10px;
    box-shadow: 0px 0px 2px #999;
    color: #fff;
    display: block;
    font-size: 12px;
    height: 18px;
    left: 100%;
    line-height: 18px;
    margin-left: 5px;
    min-width: 18px;
    padding: 0 4px;
    position: absolute;
    text-align: center;
    top: 12px;
  `,
}
